package com.example.demo.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.demo.topic.entity.TFansRecord;
import com.example.demo.topic.entity.TNoteManage;
import com.example.demo.topic.service.TFansRecordService;
import com.example.demo.topic.service.TNoteManageService;
import com.example.demo.topic.vo.TFansRecordVo;
import com.example.demo.user.entity.UComment;
import com.example.demo.user.entity.UServiceUser;
import com.example.demo.user.entity.UUser;
import com.example.demo.user.entity.UUserLogin;
import com.example.demo.user.mapper.UServiceUserMapper;
import com.example.demo.user.service.IUCommentService;
import com.example.demo.user.service.UServiceUserService;
import com.example.demo.user.service.UUserLoginService;
import com.example.demo.user.service.UUserService;
import com.example.demo.user.vo.ServiceUserVo;
import com.example.demo.util.result.Result;
import com.example.demo.util.token.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 服务人员表; 服务实现类
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@Service("uServiceUserService")
public class UServiceUserServiceImpl extends ServiceImpl<UServiceUserMapper, UServiceUser> implements UServiceUserService {

    @Autowired
    private UUserLoginService uUserLoginService;

    @Autowired
    private UServiceUserMapper uServiceUserMapper;

    @Autowired
    private TNoteManageService tNoteManageService;

    @Autowired
    private IUCommentService uCommentService;

    @Autowired
    private UUserService uUserService;

    @Autowired
    private TFansRecordService tFansRecordService;

    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * pc后台用户登录
     */
    @Override
    public Result<?> login(UUserLogin userLogin) {
        QueryWrapper<UUserLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userLogin.getUsername());
        UUserLogin user = uUserLoginService.getOne(queryWrapper);
        if(user==null){
            return Result.error().message("用户不存在");
        }
        if(!passwordEncoder.matches(userLogin.getPassword(),user.getPassword())){
            return Result.error().message("密码错误");
        }
        String token = TokenUtils.sign(user);
        return Result.ok(token).message("登录成功");
    }

    /**
     * pc后台注册
     */
    @Override
    @Transactional
    public Result<?> register(ServiceUserVo user) {
        //保存服务人员信息
        UServiceUser uServiceUser = new UServiceUser();
        BeanUtils.copyProperties(user,uServiceUser);
        save(uServiceUser);

        //保存人员登录信息
        UUserLogin uUserLogin = new UUserLogin();
        uUserLogin.setPassword(passwordEncoder.encode(user.getPassword()));//密码加密
        uUserLogin.setUsername(user.getName());
        uUserLogin.setType("0");
        uUserLogin.setBusinessId(uServiceUser.getId());
        uUserLoginService.save(uUserLogin);
        return Result.ok().message("用户注册成功");
    }

    /**
     * 账号管理模块查询数据列表
     */
    @Override
    public Result<?> getUserList(Page<ServiceUserVo> page, ServiceUserVo user) {
        QueryWrapper<ServiceUserVo> queryWrapper = new QueryWrapper<>();
        if(user.getId()!=null){
            queryWrapper.eq("id",user.getId());
        }
        if(StringUtils.isNotBlank(user.getName())){
            queryWrapper.like("name",user.getName());
        }
        if(StringUtils.isNotEmpty(user.getStatus())){
            queryWrapper.eq("status",user.getStatus());
        }
        if(StringUtils.isNotEmpty(user.getServiceType())){
            queryWrapper.eq("service_type",user.getServiceType());
        }
        queryWrapper.orderByDesc("created_time");
        return Result.ok(uServiceUserMapper.getUserList(page,queryWrapper));
    }

    @Override
    public Result<?> getUserById2(Integer id,Integer uid) {
        ServiceUserVo serviceUserVo = uServiceUserMapper.getUserById2(id);

        //获取最近发帖
        List<TNoteManage> tNoteManageList = tNoteManageService.list(new QueryWrapper<TNoteManage>().eq("master_id",id).orderByDesc("CREATED_TIME").last("limit 10"));
        if(!CollectionUtils.isEmpty(tNoteManageList)){
            serviceUserVo.setNoteManageList(tNoteManageList);
        }

        //获取评论列表
        List<UComment> uCommentList = uCommentService.list(new QueryWrapper<UComment>().eq("uid",id).orderByDesc("CREATED_TIME").last("limit 10"));

        //获取评论统计数量
        if(!CollectionUtils.isEmpty(uCommentList)){
            serviceUserVo.setCommentList(uCommentList);
            QueryWrapper<UComment> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("count(*) as allCount");
            queryWrapper.eq("uid",id);
            UComment uComment = uCommentService.getOne(queryWrapper);
            serviceUserVo.setCommentAmount(uComment.getAllCount());
        }

        //获取评论用户头像名称信息
        for(UComment uComment : uCommentList){
            UUser user = uUserService.getById(uComment.getUid());
            uComment.setAvatar(user.getAvatar());
            uComment.setName(user.getName());
        }

        //获取当前用户是否已关注
        QueryWrapper<TFansRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("master_id",id);
        wrapper.eq("fan_id",uid);
        TFansRecord tFansRecord = tFansRecordService.getOne(wrapper);
        serviceUserVo.setIzFan(tFansRecord != null);

        return Result.ok(serviceUserVo);
    }

    @Override
    public Result<?> getAdviceUserList(Page<TFansRecordVo> page) {
        return Result.ok(uServiceUserMapper.getAdviceUserList(page));
    }

}
